/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.dao.smp;

import br.gov.cemaden.library.dao.GenericDao;
import br.gov.cemaden.library.interfaces.smp.UsuarioSistemaInterface;
import br.gov.cemaden.library.model.smp.UsuarioSistema;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Simone Santos
 */
public class UsuarioSistemaDao extends GenericDao implements UsuarioSistemaInterface {

    private Session session;

    public UsuarioSistemaDao(Session session) {
        this.session = session;
    }

    public List<UsuarioSistema> findAll() {
        Transaction tx = session.beginTransaction();
        List<UsuarioSistema> user = null;
        try {
            Query query = session.getNamedQuery("UsuarioSistema.findAll");
            user = (List<UsuarioSistema>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no UsuarioSistema.findAll: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return user;
    }

    public UsuarioSistema findById(Long id) {
        Transaction tx = session.beginTransaction();
        UsuarioSistema user = null;
        try {
            Query query = session.getNamedQuery("UsuarioSistema.findById").setParameter("id", id);
            user = (UsuarioSistema) query.list();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no UsuarioSistema.findById: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return user;
    }

    public UsuarioSistema findByUser(String user) {
        Transaction tx = session.beginTransaction();
        UsuarioSistema us = null;
        try {
            Query query = session.getNamedQuery("UsuarioSistema.findByUser").setParameter("user", user);
            us = (UsuarioSistema) query.uniqueResult();
            tx.commit();
        } catch (HibernateException e) {
            tx.rollback();
            System.out.println("Erro no UsuarioSistema.findByUser: " + e.getMessage() + " - Causa: " + e.getCause());
            throw new HibernateException(e);
        }
        return us;
    }

    public void insert(UsuarioSistema user) {
        try {
            saveOrUpdate(this.session, user);
        } catch (Exception ex) {
            Logger.getLogger(WhoSysDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void update(UsuarioSistema user) {
        try {
            saveOrUpdate(this.session, user);
        } catch (Exception ex) {
            Logger.getLogger(WhoSysDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void delete(UsuarioSistema user) {
        try {
            remove(this.session, user);
        } catch (Exception ex) {
            Logger.getLogger(WhoSysDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
